package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.TmsDetectPicDto;
/**
 * 车辆检测图片信息daoimp
 * 
 * @author tianshifu
 *
 */
public class TmsDetectPicDaoImp extends TmsDao implements TmsDetectPicDao {

	protected TmsDetectPicDaoImp() throws TmsDaoException {
		super();
	}

	/**
	 * 
	 * 根据检测ID删除该车辆检测图片信息
	 * @throws TmsDaoException 
	 */
	@Override
	public void deleteTisPicInfoByDetectId(int detectId) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("DELETE FROM  ");
			sql.append(TmsTableNames.TMS_DETECT_PIC);
			sql.append(" WHERE  ");
			sql.append(" carDetectId=?  ");
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, detectId);
			ps.executeUpdate();	
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			super.close(rs, ps);
		}		

	}
     /**
      * 图片信息插入
      */
	@Override
	public void insertTisPic(TmsDetectPicDto dto) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("INSERT INTO  ");
			sql.append(TmsTableNames.TMS_DETECT_PIC);
			sql.append(" (  ");
			sql.append(" CARDETECTID,  ");
			sql.append(" CHKMODEL,  ");
			sql.append(" IMGTYPE,  ");
			sql.append(" IMGKEY,  ");
			sql.append(" PATH  ");
			sql.append(" )  ");
			sql.append(" VALUES  ");
			sql.append(" (?,?,?,?,? ) ");
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, dto.getCarDetectId());
			ps.setInt(2, dto.getChkModel());
			ps.setInt(3, dto.getImgType());
			ps.setInt(4, dto.getImgKey());
			ps.setString(5, dto.getPath());
			ps.executeUpdate();	
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			super.close(rs, ps);
		}		

		
	}

	   /**
     * 车辆图片信息查询
     * @author tianshifu
     * @说明：根据传入的DTO属性判断所要查询的结果集，可以是单条也可以是多条
     */
	@Override
	public List<TmsDetectPicDto> getTisPic(TmsDetectPicDto dto) throws TmsDaoException {
		List<TmsDetectPicDto> list = new ArrayList<TmsDetectPicDto>(); 
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("SELECT  ");
			//
			sql.append(" ID,  ");
			sql.append(" CARDETECTID,  ");
			sql.append(" CHKMODEL,     ");
			sql.append(" IMGTYPE,      ");
			sql.append(" IMGKEY,  ");
			sql.append(" PATH  ");
			sql.append(" FROM   ");
			sql.append(TmsTableNames.TMS_DETECT_PIC);
			if(dto!=null){
				sql.append(" WHERE  ");
				if(dto.getId()>0){
					sql.append(" ID="+dto.getId()+"  ");
					if(dto.getCarDetectId()>0||dto.getChkModel()>0||dto.getImgKey()>0||dto.getImgType()>0){
						sql.append(" AND  ");	
					}
				} if(dto.getCarDetectId()>0){
					sql.append(" CARDETECTID="+dto.getCarDetectId()+"  ");
					if(dto.getChkModel()>0||dto.getImgKey()>0||dto.getImgType()>0){
						sql.append(" AND  ");	
					}
				} if(dto.getChkModel()>0){
					sql.append(" CHKMODEL="+dto.getChkModel()+"  ");
					if(dto.getImgKey()>0||dto.getImgType()>0){
						sql.append(" AND  ");	
					}
				} if(dto.getImgType()>0){
					sql.append(" IMGTYPE="+dto.getImgType()+"  ");
					if(dto.getImgKey()>0){
						sql.append(" AND  ");	
					}
				} if(dto.getImgKey()>0){
					sql.append(" IMGKEY="+dto.getImgKey()+"  ");
				}
			}
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			
			while (rs.next()) {
				dto = new TmsDetectPicDto();
		        dto.setId(rs.getInt("ID"));
		        dto.setCarDetectId(rs.getInt("CARDETECTID"));
		        dto.setChkModel(rs.getInt("CHKMODEL"));
		        dto.setImgType(rs.getInt("IMGTYPE"));
		        dto.setImgKey(rs.getInt("IMGKEY"));
		        dto.setPath(rs.getString("PATH"));

				list.add(dto);
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			super.close(rs, ps);
		}		

		return list;
	}
	/**
	 * 
	 * 根据车辆检测ID查询车辆检测图片信息
	 * @throws TmsDaoException 
	 */
	@Override
	public List<TmsDetectPicDto> findDetectPicInfoByDetectIdAndType(int carDetectId,int imgType) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		TmsDetectPicDto dto = null;
		List<TmsDetectPicDto> dtoList = new ArrayList<TmsDetectPicDto>();
		
		try {
			conn = super.getConnection();
			
			StringBuffer sql = new StringBuffer();
			sql.append("SELECT ");
			sql.append(" CHKMODEL,  	");
			sql.append(" IMGTYPE,  		");
			sql.append(" IMGKEY,  		");
			sql.append(" PATH  			");
			sql.append(" FROM 			");
			sql.append(TmsTableNames.TMS_DETECT_PIC);
			sql.append(" WHERE  CARDETECTID=? ");
			sql.append(" AND  IMGTYPE=? ");

			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, carDetectId);
			ps.setInt(2, imgType);
			
			// 执行sql
			rs = ps.executeQuery();

			while (rs.next()) {
				dto = new TmsDetectPicDto();
				dto.setChkModel(rs.getInt("CHKMODEL"));
				dto.setImgType(rs.getInt("IMGTYPE"));
				dto.setImgKey(rs.getInt("IMGKEY"));
				dto.setPath(rs.getString("PATH"));

				dtoList.add(dto);
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
			
		} finally {
			super.close(rs,ps);
		}
		return dtoList;
	}

	/**
	 * 
	 * 批量添加图片信息
	 * @throws TmsDaoException 
	 */
	@Override
	public void insertTisDetectPicInfo(List<TmsDetectPicDto> dtoList) throws TmsDaoException {
		
		PreparedStatement ps = null;
		ResultSet rs = null;
		TmsDetectPicDto dto = null;
		try {
			StringBuffer sql = new StringBuffer("INSERT INTO ");
			sql.append(TmsTableNames.TMS_DETECT_PIC);
			sql.append("(");
			sql.append(" CARDETECTID, 	");
			sql.append(" CHKMODEL, 		");
			sql.append(" IMGTYPE, 		");
			sql.append(" IMGKEY, 		");
			sql.append(" PATH 			");
			sql.append("  ) VALUES ( 	");
			sql.append(" ?,?,?,?,? )		");

			ps = conn.prepareStatement(sql.toString());
			
			for(int i=0;i<dtoList.size();i++)
			{
				dto = (TmsDetectPicDto)dtoList.get(i);
				ps.setInt(1, dto.getCarDetectId());
				ps.setInt(2, dto.getChkModel());
				ps.setInt(3, dto.getImgType());
				ps.setInt(4, dto.getImgKey());
				ps.setString(5, dto.getPath());
				ps.executeUpdate();

			}
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}		
	}

	@Override
	public List<TmsDetectPicDto> findDetectPicInfoByDetectId(int carDetectId)
			throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		TmsDetectPicDto dto = null;
		List<TmsDetectPicDto> dtoList = new ArrayList<TmsDetectPicDto>();
		
		try {
			conn = super.getConnection();
			
			StringBuffer sql = new StringBuffer();
			sql.append("SELECT ");
			sql.append(" ID,  	");
			sql.append(" CHKMODEL,  	");
			sql.append(" IMGTYPE,  		");
			sql.append(" IMGKEY,  		");
			sql.append(" PATH, 			");
			sql.append(" CARDETECTID 	");
			sql.append(" FROM 			");
			sql.append(TmsTableNames.TMS_DETECT_PIC);
			sql.append(" WHERE  CARDETECTID=? ");

			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, carDetectId);
			
			// 执行sql
			rs = ps.executeQuery();

			while (rs.next()) {
				dto = new TmsDetectPicDto();
				dto.setId(rs.getInt("ID"));
				dto.setChkModel(rs.getInt("CHKMODEL"));
				dto.setImgType(rs.getInt("IMGTYPE"));
				dto.setImgKey(rs.getInt("IMGKEY"));
				dto.setPath(rs.getString("PATH"));
				dto.setCarDetectId(rs.getInt("CARDETECTID"));

				dtoList.add(dto);
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
			
		} finally {
			super.close(rs,ps);
		}
		return dtoList;
	}

}
